package com.ruike.alisurface.utils;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import com.voodoo.lib_logcat.L;

public class MyTextWatcher implements TextWatcher {
    private EditText editText;
    private String mBefore;// 用于记录变化前的文字
    private int mCursor;// 用于记录变化时光标的位置

    public MyTextWatcher(EditText editText) {
        this.editText = editText;
    }

 
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        mBefore = s.toString();
        mCursor = start;
        L .d(  "beforeTextChanged: " + s + ", " + mCursor);
    }
 
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }
 
    @Override
    public void afterTextChanged(Editable s) {
        L.d( "afterTextChanged: " + s);
        // 这里图方便，是在afterTextChanged判断是否输入之后含有空格，并不是最好的方案
        // 理论上应该在onTextChanged中判断输入内容
        if (s.toString().contains(" ")) {
            L .i( "onTextChanged: 包含空格, 设置" + mBefore);
            editText.removeTextChangedListener(this);
            editText.setText(mBefore);
            editText.addTextChangedListener(this);
            editText.setSelection(mCursor);
        }
    }
}